package com.google.code.mochaccino.framework.ui.demo;
/*
 * Copyright 2012 Claude Houle claude.houle@gmail.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import com.google.code.mochaccino.framework.freemarker.TemplateConfiguration;
import com.google.code.mochaccino.framework.freemarker.TemplateConfigurationProvider;
import com.google.code.mochaccino.framework.freemarker.TemplateManager;
import com.google.code.mochaccino.framework.rest.Root;
import com.google.code.mochaccino.framework.rest.mime.MimeTypes;
import com.google.code.mochaccino.framework.rest.resources.ResourceClasspath;
import com.google.code.mochaccino.framework.rest.resources.ResourceLoaderImpl;
import com.google.code.mochaccino.framework.rest.resources.ResourceService;
import com.google.inject.Provides;
import com.google.inject.name.Names;
import com.google.inject.util.Providers;
import com.sun.jersey.guice.JerseyServletModule;
import com.sun.jersey.guice.spi.container.servlet.GuiceContainer;
import freemarker.cache.ClassTemplateLoader;
import freemarker.cache.TemplateLoader;
import freemarker.ext.beans.BeansWrapper;
import freemarker.template.ObjectWrapper;
import java.io.File;
import java.io.IOException;
import java.util.UUID;
import javax.inject.Singleton;
import org.apache.commons.io.FileUtils;

/**
 *
 */
public class DemoModule extends JerseyServletModule {

	@Override
	protected void configureServlets() {

		// Template
		bind( TemplateConfiguration.class ).toProvider( Providers.guicify( new TemplateConfigurationProvider() ) ).in( Singleton.class );
		bind( ObjectWrapper.class ).toInstance( BeansWrapper.getDefaultInstance() );
		bind( TemplateLoader.class ).toInstance( new ClassTemplateLoader( TemplateLoader.class, "/" ) );
		bind( TemplateManager.class ).in( Singleton.class );
		bind( String.class ).annotatedWith( Names.named( Root.NAME_ROOT_REDIRECT_PATH ) ).toInstance( "/demo/button.html" );
		bind( Root.class );
		bind( Demo.class );
		serve( "/*" ).with( GuiceContainer.class );
	}

	@Singleton
	@Provides
	ResourceService provideResourceService() {
		final File tempDir = new File( System.getProperty( "java.io.tempdir" ), UUID.randomUUID().toString() );
		Runtime.getRuntime().addShutdownHook( new Thread( new Runnable() {
			@Override
			public void run() {
				try {
					FileUtils.deleteDirectory( tempDir );
				} catch ( IOException e ) {
					System.err.println( e.getMessage() );
					e.printStackTrace( System.err );
				}
			}
		} ) );

		ResourceService service = new ResourceService( new ResourceLoaderImpl( tempDir, new ResourceClasspath( "web" ) ) );
		service.addMimeType( MimeTypes.ICO );
		service.addMimeType( MimeTypes.GIF );
		service.addMimeType( MimeTypes.JPEG );
		service.addMimeType( MimeTypes.PNG );
		service.addMimeType( MimeTypes.JPG );
		service.addMimeType( MimeTypes.CSS );
		service.addMimeType( MimeTypes.HTML );
		service.addMimeType( MimeTypes.JS );
		service.addMimeType( MimeTypes.LESS );
		return service;
	}
}
